﻿@using StackExchange.Opserver.Data.Redis
@model RedisMemoryAnalysis
@{
    Layout = "~/Views/Shared/Master.cshtml";
    var serverInfo = RedisInstance.GetInstance(Model.ConnectionInfo);
}
@helper TopKeys(KeyStats ks) {
    foreach (var tk in ks.TopKeys)
    {
@:@tk.Value (@tk.Key.ToComma() bytes)
    }
}
@helper RenderSlaves(RedisInstance serverInfo, int nest = 0)
{
    if (serverInfo.SlaveCount <= 0) { return; }
    foreach (var ci in serverInfo.SlaveInstances)
    {
        <li style="padding-left: @(nest * 20)px">@ci.IconSpan() <a href="/redis/analyze/memory?node=@ci.Host:@ci.Port&db=@Model.Database" class="node-name-link">@ci.Host</a> <span class="note">(@ci.Port)</span></li>
        @RenderSlaves(ci, ++nest)
    }
}
@functions {
}

<div class="node-summary redis-summary redis-infos-wrap">
    <table class="cluster-dashboard striped-dashboard">
        <thead class="node-header" data-name="header-missing">
            <tr class="category-row">
                <th colspan="5">
                    <h3>
                        @Model.IconSpan() Memory Analysis of @Model.ConnectionInfo.Name
                        <span class="cluster-info">
                            (@Model.ConnectionInfo.Host:@Model.ConnectionInfo.Port - Database @Model.Database)
                            - <span title="@(((int)Model.TotalTime.TotalMilliseconds).ToComma()) ms">Runtime: @Model.TotalTime.ToReadableString()</span>
                            - Run @Model.CreationDate.ToRelativeTimeSpan()
                        </span>
                        <a style="float: right;" href="/redis/analyze/memory/clear?node=@serverInfo.Host:@serverInfo.Port&db=@Model.Database">Re-run analysis</a>
                    </h3>
                </th>
            </tr>
            <tr>
                <th>Key Pattern</th>
                <th>Count</th>
                <th>Bytes (Key)</th>
                <th>Bytes (Value)</th>
                <th>Bytes (Total)</th>
            </tr>
        </thead>
        <tbody class="node-group" data-name="body-missing">
            @if (Model.ErrorMessage.HasValue())
            {
                <tr class="redis-error">
                    <td colspan="5">Error: @Model.ErrorMessage</td>
                </tr>
            }
            @if (serverInfo.IsMaster && serverInfo.SlaveCount > 0 && Model.KeyStats.Count == 1)
            {
                <tr class="option-row">
                    <td colspan="5">
                        <div class="alternative-masters">
                            Run on one of these slaves instead?
                            <ul>
                                @RenderSlaves(serverInfo)
                            </ul>
                        </div>
                    </td>
                </tr>
            }
            @foreach (var s in Model.KeyStats.OrderByDescending(s => s.Value.TotalByteSize))
            {
                var ks = s.Value;
                if (s.Value.Count == 0)
                {
                    continue;
                }
                <tr>
                    <td title="Regex: @s.Key.Regex.ToString()
                        @TopKeys(s.Value) ">
                        @s.Key.Name
                    </td>
                    <td>@ks.Count.ToComma()</td>
                    <td>@ks.KeyByteSize.ToComma()</td>
                    <td>@ks.ValueByteSize.ToComma()</td>
                    <td>@ks.TotalByteSize.ToComma()</td>
                </tr>
            }
        </tbody>
        <tfoot>
            <tr class="total-row">
                <td><b>Total</b></td>
                <td>@Model.Count.ToComma()</td>
                <td>@Model.KeyByteSize.ToHumanReadableSize()</td>
                <td>@Model.ValueByteSize.ToHumanReadableSize()</td>
                <td>@Model.TotalByteSize.ToHumanReadableSize()</td>
            </tr>
        </tfoot>
    </table>
@if (Model.TopKeys.Any())
{
    <table class="cluster-dashboard striped-dashboard">
        <thead class="node-header" data-name="header-missing">
            <tr class="category-row">
                <th colspan="5">
                    <div>Top Keys (by Size) for @Model.ConnectionInfo.Name</div>
                </th>
            </tr>
            <tr>
                <th>Category</th>
                <th>Key</th>
                <th>Bytes (Key)</th>
                <th>Bytes (Value)</th>
                <th>Bytes (Total)</th>
            </tr>
        </thead>
        <tbody class="node-group" data-name="body-missing">
            @foreach (var tk in Model.TopKeys)
            {
                <tr>
                    <td>@tk.Matcher.Name</td>
                    <td>@tk.Name</td>
                    <td>@tk.KeyBytes.ToComma()</td>
                    <td>@tk.ValueBytes.ToComma()</td>
                    <td>@tk.TotalBytes.ToComma()</td>
                </tr>
            }
        </tbody>
    </table>
}
</div>